package hn.sigit.logic.security;

import hn.sigit.logic.bpm.ActorType;
import hn.sigit.model.hnd.administrative.HND_UserRoleType;
import hn.sigit.model.hnd.administrative.HND_User;
import hn.sigit.dao.hnd.administrative.HND_UserDAO;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.ejb.Stateless;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.bpm.Actor;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;


@Stateless
@Name("authenticator")
public class Authenticator implements IAuthenticator {
	@Logger
	private Log log;

	@In
	Identity identity;
	@In
	Credentials credentials;
	@In(create = true)
	private Actor actor;

	@Out(scope = ScopeType.SESSION, required = false)
	private String loggedUserFullName;

	public static String MD5(String md5) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; i++)
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));

			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return md5;
	}

	public boolean authenticate() {
		log.info("authenticating {0}", credentials.getUsername());

		String userName = credentials.getUsername();
		String password = MD5(credentials.getPassword());

		HND_User user = HND_UserDAO.loadUser(userName, password);

		if (user != null && user.getActive()) {
			loggedUserFullName = user.getParty().getFullNameLastNameFirst();
			actor.setId(userName);

			if (user.getReceptionist()) {
				identity.addRole(HND_UserRoleType.RECEPTIONIST.toString());
				actor.getGroupActorIds().add(ActorType.RECEPTIONIST.toString());
			}

			if (user.getAnalyst()) {
				identity.addRole(HND_UserRoleType.ANALYST.toString());
				actor.getGroupActorIds().add(ActorType.ANALYST.toString());
				actor.getGroupActorIds().add(ActorType.ANALYST_OR_APPROVER.toString());
			}
			
			if (user.getEditor()) {
				identity.addRole(HND_UserRoleType.EDITOR.toString());
				actor.getGroupActorIds().add(ActorType.EDITOR.toString());
			}
			
			if (user.getApprover()) {
				identity.addRole(HND_UserRoleType.APPROVER.toString());
				actor.getGroupActorIds().add(ActorType.APPROVER.toString());
				actor.getGroupActorIds().add(ActorType.ANALYST_OR_APPROVER.toString());
			}

			if (user.getSurveyingTechnician()) {
				identity.addRole(HND_UserRoleType.SURVEYING_TECHNICIAN.toString());
			}
			
			if (user.getExternalQuerier()) {
				identity.addRole(HND_UserRoleType.EXTERNAL_QUERIER.toString());
			}
			
			if (user.getInternalQuerier()) {
				identity.addRole(HND_UserRoleType.INTERNAL_QUERIER.toString());
			}
			
			if (user.getManager()) {
				identity.addRole(HND_UserRoleType.MANAGER.toString());
			}
			
			if (user.getSystemAdministrator()) {
				identity.addRole(HND_UserRoleType.SYSTEM_ADMINISTRATOR.toString());
			}
			
			return true;
		}

		return false;
	}

	@Override
	public boolean checkIfLoogedIn() {
		return identity.isLoggedIn();
	}

	@Override
	public void destroy() {
	}
}
